from pandac.PandaModules import TextNode
from panda3d.core import Vec2, Point3, Vec3, Vec4
from pandac.PandaModules import TransparencyAttrib
'''
	Andrew Oberlin
	Date: March 13, 2012
'''
class GameStats:
	'''
		Creates the statistics for resources up in
		the right corner of the screen
		
		Params: parent -- the current object for singleplayer
	'''
	def __init__(self, parent):
		self.parent = parent
		self.aspect2d = self.parent.parent.base.aspect2d
		self.minerals = 1000
		self.vespene = 0
		self.unitsAvailable = 10
		self.unitsUsed = 1
		
		self.count = 0
		self.statsText = dict()
		
		self.__loadGui()
		
	'''
		Loads the gui to display the
		statistics being kept
	'''
	def __loadGui(self):
		self.dummy = self.aspect2d.attachNewNode('game_stats_dummy')
		self.dummy.setPos(0.75, 0, 0.95)
		
		# minerals label image
		self.mineralModel = self.parent.parent.loader.loadModelCopy("models/plane")
		self.mineralModel.setTexture(self.parent.parent.loader.loadTexture("models/terrain/minerals.png"))
		self.mineralModel.setTransparency(1)
		self.mineralModel.reparentTo(self.dummy)
		self.mineralModel.setScale(0.1)
		self.mineralModel.setPos(-0.5, 0, 0)
		
		# minerals label
		self.statsText['minerals'] = TextNode('minerals_stats_text')
		self.statsText['minerals'].setText(str(self.minerals))
		self.statsText['minerals'].setAlign(TextNode.ABoxedLeft)
		textModel = self.dummy.attachNewNode(self.statsText['minerals'])
		textModel.setPos(-0.45, 0, -0.025)
		textModel.setScale(0.075)
		
		# vespene label image
		self.vespeneModel = self.parent.parent.loader.loadModelCopy("models/plane")
		self.vespeneModel.setTexture(self.parent.parent.loader.loadTexture("models/terrain/vespene.png"))
		self.vespeneModel.setTransparency(1)
		self.vespeneModel.reparentTo(self.dummy)
		self.vespeneModel.setScale(Vec3(0.1, 0.1, 0.075))
		self.vespeneModel.setPos(-0.1, 0, 0)
		
		# vespene label
		self.statsText['vespene'] = TextNode('vespene_stats_text')
		self.statsText['vespene'].setText(str(self.vespene))
		self.statsText['vespene'].setAlign(TextNode.ABoxedLeft)
		textModel = self.dummy.attachNewNode(self.statsText['vespene'])
		textModel.setPos(-0.05, 0, -0.025)
		textModel.setScale(0.075)
		
		# units available label image
		self.unitsModel = self.parent.parent.loader.loadModelCopy("models/plane")
		self.unitsModel.setTexture(self.parent.parent.loader.loadTexture("models/menu/hud/units_available.png"))
		#self.unitsModel.setTransparency(1)
		self.unitsModel.reparentTo(self.dummy)
		self.unitsModel.setScale(0.075)
		self.unitsModel.setPos(0.25, 0, 0)
		
		# units label
		self.statsText['units'] = TextNode('units_stats_text')
		self.statsText['units'].setText(str(self.unitsUsed) + "/" + str(self.unitsAvailable))
		self.statsText['units'].setAlign(TextNode.ABoxedLeft)
		textModel = self.dummy.attachNewNode(self.statsText['units'])
		textModel.setPos(0.30, 0, -0.025)
		textModel.setScale(0.075)
	
	'''
		Adds to the current state of the mineral stores
	'''
	def addMinerals(self, newMins):
		self.minerals += newMins
		self.statsText['minerals'].setText(str(self.minerals))

		
	'''
		Adds to the current state of the vespene stores
	'''
	def addVespene(self, newVesp):
		self.vespene += newVesp
		self.statsText['vespene'].setText(str(self.vespene))
		
	'''
		Adds to the current state of the available
		units. Adds 5 new units to be available
	'''
	def addUnitsAvailable(self):
		self.unitsAvailable += 5
		self.statsText['units'].setText(str(self.unitsUsed) + "/" + str(self.unitsAvailable))
	
	'''
		Adds to the current state of the available
		units. Adds 5 new units to be available
	'''
	def addUnitsUsed(self, units):
		self.unitsUsed += units
		self.statsText['units'].setText(str(self.unitsUsed) + "/" + str(self.unitsAvailable))
	
	'''
		Checks to see if a purchase of any kind is valid
		
		Returns True if successful, and False if unsuccessful
		and also tells the player
	'''
	def purchase(self, minerals, vespene, units):
		success = False
		self.count += 1
		textNodeAlert = TextNode('alert_' + str(self.count))
		if (self.minerals - minerals >= 0):
			if (self.vespene - vespene >= 0):
				if (self.unitsUsed + units <= self.unitsAvailable):
					success = True
				else:
					textNodeAlert.setText("Not enough supplies. Need more supply depots!")
					success = False
			else:
				textNodeAlert.setText("Not enough vespene gas!")
				success = False
		else:
			textNodeAlert.setText("Not enough minerals!")
			success = False
			
		if (success):
			self.addMinerals(-minerals)
			self.addVespene(-vespene)
			self.addUnitsUsed(units)
			self.count -= 1
			
			return success
		else:
			textModel = self.aspect2d.attachNewNode(textNodeAlert)
			textModel.setPos(0, 0, 0.75)
			textModel.setScale(0.075)
			textModel.setTransparency(TransparencyAttrib.MAlpha)
			
			task = self.parent.parent.taskMgr.add(self.disappear, 'alert_' + str(self.count))
			task.color = Vec4(1, 1, 1, 1)
			task.rate = 0.15
			task.prevTime = 0
			task.textModel = textModel
			
			return success


	def disappear(self, task):
		deltaT = task.time - task.prevTime
		task.prevTime = task.time
		task.color[3] -= task.rate*deltaT
		if (task.color[3] > 0):
			task.textModel.setColor(task.color)
			return task.cont
		else:
			task.textModel.removeNode()
			return task.done
			
	def destroy(self):
		self.dummy.removeNode()
		